package com.example.mall.order.controller;

import com.example.mall.common.utils.R;
import com.example.mall.order.entity.OrderEntity;
import com.example.mall.order.entity.OrderItemEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("order/rabbitmq")
@Slf4j
public class RabbitMqController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/sendMessageForOrderEntity")
    public R sendMessageForOrderEntity() {
        for (int i = 0; i < 5; i++) {
            OrderEntity order = new OrderEntity();
            order.setOrderSn("111" + i);
            order.setNote("哈哈哈");
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:ss:mm");
            String format = simpleDateFormat.format(new Date());
            order.setReceiverDetailAddress(format);
            rabbitTemplate.convertAndSend("exchange.direct", "twotwo", order);
        }
        return R.ok().put("message", "消息发送成功");
    }

    @GetMapping("/sendMessageForOrderItemEntity")
    public R sendMessageForOrderItemEntity() {
        OrderItemEntity orderItemEntity = new OrderItemEntity();
        orderItemEntity.setOrderSn("222");
        orderItemEntity.setSkuName("嘿嘿嘿");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:ss:mm");
        String format = simpleDateFormat.format(new Date());
        orderItemEntity.setSpuName(format);
        rabbitTemplate.convertAndSend("exchange.direct", "twotwo", orderItemEntity);
        return R.ok().put("message", "消息发送成功");
    }

    @GetMapping("/sendMessageFailedForOrderItemEntity")
    public R sendMessageFailedForOrderItemEntity() {
        OrderItemEntity orderItemEntity = new OrderItemEntity();
        orderItemEntity.setOrderSn("333");
        orderItemEntity.setSkuName("嘻嘻嘻");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:ss:mm");
        String format = simpleDateFormat.format(new Date());
        orderItemEntity.setSpuName(format);
        rabbitTemplate.convertAndSend("exchange.direct", "xxx", orderItemEntity);
        return R.ok().put("message", "消息发送成功");
    }

    @GetMapping("/sendMessageForOrderEntity2")
    public R sendMessageForOrderEntity2() {
        OrderEntity order = new OrderEntity();
        order.setOrderSn("444");
        order.setNote("啧啧啧");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:ss:mm");
        String format = simpleDateFormat.format(new Date());
        order.setReceiverDetailAddress(format);
        rabbitTemplate.convertAndSend("event.exchange", "delay.queue", order);
        return R.ok().put("message", "消息发送成功");
    }

}
